<?php
use yii\helpers\Url;
$web = Url::base();
$this->title = '系统商品池';
?>
<script type="text/javascript" src="<?= $web ?>/js/jquery.tooltips.js"></script>
<script type="application/javascript">

    //同步产品
    function synGoods() {
        var result = confirm('确定要同步业务系统产品?');
        if (result == true) {
            loading.show();
            requestJson("syngoods-handler", null, true, function (response) {
                loading.hide();
                if (response.status == 0) {
//                    alert('同步成功！');
                    filter(1);
                } else {
                    alert('同步失败：' + response.message);
                }
            });
        }
    }

    //同步产品基础信息
    function synGoodsBaseinfo() {
        var result = confirm('确定要同步业务系统产品基础信息?');
        if (result == true) {
            loading.show();
            requestJson("syngoods-baseinfo-handler", null, true, function (response) {
                loading.hide();
                if (response.status == 0) {
                    filter(1);
                } else {
                    alert('同步失败：' + response.message);
                }
            });
        }
    }

    //同步产品HTML
    function synGoodsHtml() {
        var result = confirm('确定要同步业务系统产品详情描述?');
        if (result == true) {
            loading.show();
            requestJson("syngoods-html-handler", null, true, function (response) {
                loading.hide();
                if (response.status == 0) {
                    filter(1);
                } else {
                    alert('同步失败：' + response.message);
                }
            });
        }
    }

    //同步产品基础信息
    function synGoodsImage() {
        var result = confirm('确定要同步业务系统产品图片?');
        if (result == true) {
            loading.show();
            requestJson("syngoods-image-handler", null, true, function (response) {
                loading.hide();
                if (response.status == 0) {
                    filter(1);
                } else {
                    alert('同步失败：' + response.message);
                }
            });
        }
    }


    //筛选
    function filter(filtertype) {
        var code = '';
        var name = '';
        var group_code = '';
        var group_item_name = '';
        var brand = '';
        var type = '';
        var is_enabled = '';
        var is_shelved = '';
        var is_activity = '';
        var is_trusteeship = '';
        var jg_form =  '';
        var jg_to =  '';
        if (filtertype == 1) {
            code = $("#head_code").val();
            name = $("#head_name").val();
            is_enabled = $("#head_is_enabled").val();
            is_shelved = $("#head_is_shelved").val();
        } else {
            code = $("#dlg_code").val();
            name = $("#dlg_name").val();
            group_code = $("#dlg_group_code").val();
            group_item_name = $("#dlg_group_item_name").val();
            brand = $("#dlg_brand").val();
            type = $("#dlg_type").val();
            is_enabled = $("#dlg_is_enabled").val();
            is_shelved = $("#dlg_is_shelved").val();
            is_activity = $("#dlg_is_activity").val();
            is_trusteeship = $("#dlg_is_trusteeship").val();
            jg_form = $("#dlg_jg_form").val();
            jg_to = $("#dlg_jg_to").val();
        }

        var href = window.location.href;
        href = removeUrlAllParams(href);
        href = setUrlParam(href, 'page', 1);

        if (code != '' && code != null) {
            href = setUrlParam(href, 'code', code);
        }
        if (name != '' && name != null) {
            href = setUrlParam(href, 'name', name);
        }
        if (group_code != '' && group_code != null) {
            href = setUrlParam(href, 'group_code', group_code);
        }
        if (group_item_name != '' && group_item_name != null) {
            href = setUrlParam(href, 'group_item_name', group_item_name);
        }
        if (brand != '' && brand != null) {
            href = setUrlParam(href, 'brand', brand);
        }
        if (type != '' && type != null) {
            href = setUrlParam(href, 'type', type);
        }
        if (is_enabled != '' && is_enabled != null) {
            href = setUrlParam(href, 'enabled', is_enabled);
        }
        if (is_shelved != '' && is_shelved != null) {
            href = setUrlParam(href, 'shelved', is_shelved);
        }
        if (is_activity != '' && is_activity != null) {
            href = setUrlParam(href, 'activity', is_activity);
        }
        if (is_trusteeship != '' && is_trusteeship != null) {
            href = setUrlParam(href, 'trusteeship', is_trusteeship);
        }
        if (jg_form != '' && jg_form != null) {
            href = setUrlParam(href, 'jg_form', jg_form);
        }
        if (jg_to != '' && jg_to != null) {
            href = setUrlParam(href, 'jg_to', jg_to);
        }

        window.location.href = href;
    }

    //展示产品详细信息
    function show_goods_detail(goods_code, institution_code) {
        goods_code = encodeURI(goods_code);
        var data = {};
        data.goods_code = goods_code;
        data.institution_code = institution_code;

        $("#goodsdetail").html('');
        requestHtml('goods-detail-part-view', data, true, function (responseHtml) {
            $("#goodsdetail").html(responseHtml);
        }, function () {
            $("#goodsdetail").html('无产品详情数据 ');
        });
    }

    //展示产品HTML详细信息
    function show_html_detail(goods_code, institution_code) {
        goods_code = encodeURI(goods_code);
        var data = {};
        data.goods_code = goods_code;
        data.institution_code = institution_code;

        $("#htmldetail").html('');
        requestHtml('goods-htmldetail-part-view', data, true, function (responseHtml) {
            $("#htmldetail").html(responseHtml);
        }, function () {
            $("#htmldetail").html('无产品详情数据 ');
        });
    }

</script>

<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>

<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button class="btn btn-primary" type="button" onclick="synGoods()"><i class="fa fa-refresh"></i> 同步业务系统产品
                </button>
                <button class="btn btn-primary" type="button" onclick="synGoodsBaseinfo()"><i class="fa fa-refresh"></i> 同步产品基础信息
                </button>
                <button class="btn btn-primary" type="button" onclick="synGoodsHtml()"><i class="fa fa-refresh"></i> 同步产品详情描述
                </button>
                <button class="btn btn-primary" type="button" onclick="synGoodsImage()"><i class="fa fa-refresh"></i> 同步产品图片
                </button>
            </div>
        </div>
    </div>
</div>
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="pull-right" style=" margin-top: 5px">
                <div class="form-group">
                    <label class="sr-only" for="exampleInputEmail2">商品编码</label>
                    <input type="text" class="form-control" id="head_code" placeholder="商品编码" value="<?= $code ?>">
                </div>
                <div class="form-group">
                    <label class="sr-only" for="exampleInputPassword2">商品名称</label>
                    <input type="text" class="form-control" id="head_name" placeholder="商品名称" value="<?= $name ?>">
                </div>
                <select id="head_is_enabled" class="form-control" onchange="">
                    <option value="" <?php if ($enabled == "-1") echo "selected=\"selected\""; ?>>是否启用</option>
                    <option value="1" <?php if ($enabled == "1") echo "selected=\"selected\""; ?>>启用</option>
                    <option value="0" <?php if ($enabled == "0") echo "selected=\"selected\""; ?>>禁用</option>
                </select>
                <select id="head_is_shelved" class="form-control" onchange="">
                    <option value="" <?php if ($shelved == "-1") echo "selected=\"selected\""; ?>>是否上架</option>
                    <option value="1" <?php if ($shelved == "1") echo "selected=\"selected\""; ?>>已经上架</option>
                    <option value="0" <?php if ($shelved == "0") echo "selected=\"selected\""; ?>>未上架</option>
                </select>
                <button type="button" class="btn btn-primary" onclick="filter(1)"><i class="fa fa-search"></i> 搜索
                </button>
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModalFilter">
                    高级搜索
                </button>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="goodsList">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>商品图片</th>
                    <th>商品编码</th>
                    <th>商品名称</th>
                    <th>差异名称</th>
                    <th>结算价格</th>
                    <th>销售价格</th>
                    <th>供应价格</th>
                    <th>是否启用</th>
                    <th>是否上架</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && $list != '') {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td  align="center" style="vertical-align:middle;">
                                <a href="javascript:void(0)" class="tooltips" rel="<?= $list[$i]['goods_image'] ?>" target="_blank" >
                                    <img src="<?= $list[$i]['goods_image'] ?>" style="width: 3rem; height: 3rem;">
                                </a>
                            </td>
                            <td><a href="#" onclick="show_goods_detail('<?= $list[$i]['code'] ?>','<?= $list[$i]['institution_code'] ?>')" data-toggle="modal"
                                   data-target="#myModalGoodsDetail"><?= $list[$i]['code'] ?></a></td>
                            <td><?= $list[$i]['name'] ?></td>
                            <td><?= $list[$i]['group_item_name'] ?></td>
                            <td><?= $list[$i]['settlement_price'] ?></td>
                            <td><?= $list[$i]['selling_price'] ?></td>
                            <td><?= $list[$i]['supply_price'] ?></td>
                            <td><?php if ($list[$i]["is_enabled"] == 1) {
                                    echo '<span class="label label-success">启用</span>';
                                } else {
                                    echo '<span class="label label-danger">禁用</span>';
                                } ?></td>
                            <td><?php if ($list[$i]["is_shelved"] == 1) {
                                    echo '<span class="label label-success">已经上架</span>';
                                } else {
                                    echo '<span class="label label-default">未上架</span>';
                                } ?></td>
                            <td><a href="#" class="btn btn-link" onclick="show_html_detail('<?= $list[$i]['code'] ?>','<?= $list[$i]['institution_code'] ?>')"
                                   data-toggle="modal"
                                   data-target="#myModalHtmlDetail">网页详情</a></td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>
<!-- Modal filter-->
<div class="modal fade" id="myModalFilter" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">高级搜索</h4>
            </div>
            <div id="modalFilter" class="modal-body">
                <form class="form-inline" role="form">
                    <div class="form-group">
                        <label for="exampleInputEmail2">商品编码：</label>
                        <input type="text" class="form-control" style="width: 203px;" id="dlg_code" placeholder=""
                               value="<?= $code ?>">

                        <label for="exampleInputPassword2">商品名称：</label>
                        <input type="text" class="form-control" style="width: 203px;" id="dlg_name" placeholder=""
                               value="<?= $name ?>">
                    </div>
                    <div class="form-group" style=" margin-top: 3px">
                        <label for="exampleInputEmail2">同款编码：</label>
                        <input type="text" class="form-control" style="width: 203px;" id="dlg_group_code" placeholder=""
                               value="<?= $group_code ?>">

                        <label for="exampleInputPassword2">差异名称：</label>
                        <input type="text" class="form-control" style="width: 203px;" id="dlg_group_item_name" placeholder=""
                               value="<?= $group_item_name ?>">
                    </div>
                    <div class="form-group" style=" margin-top: 3px">
                        <label for="exampleInputEmail2">商品品牌：</label>
                        <input type="text" class="form-control" style="width: 203px;" id="dlg_brand" placeholder=""
                               value="<?= $brand ?>">

                        <label for="exampleInputPassword2">商品类型：</label>
                        <select id="dlg_type" class="form-control" style="width: 203px;" onchange="">
                            <option value="" <?php if ($type == "") echo "selected=\"selected\""; ?>></option>
                            <option value="1" <?php if ($type == "1") echo "selected=\"selected\""; ?>>实物</option>
                            <option value="2" <?php if ($type == "2") echo "selected=\"selected\""; ?>>虚拟商品</option>
                        </select>
                    </div>
                    <div class="form-group" style=" margin-top: 3px">
                        <label for="exampleInputEmail2">是否启用：</label>
                        <select id="dlg_is_enabled" class="form-control" style="width: 203px;" onchange="">
                            <option value="" <?php if ($enabled == "-1") echo "selected=\"selected\""; ?>></option>
                            <option value="1" <?php if ($enabled == "1") echo "selected=\"selected\""; ?>>启用</option>
                            <option value="0" <?php if ($enabled == "0") echo "selected=\"selected\""; ?>>禁用</option>
                        </select>
                        <label for="exampleInputEmail2">是否上架：</label>
                        <select id="dlg_is_shelved" class="form-control" style="width: 203px;" onchange="">
                            <option value="" <?php if ($shelved == "-1") echo "selected=\"selected\""; ?>></option>
                            <option value="1" <?php if ($shelved == "1") echo "selected=\"selected\""; ?>>已经上架</option>
                            <option value="0" <?php if ($shelved == "0") echo "selected=\"selected\""; ?>>未上架</option>
                        </select>
                    </div>
                    <br>
                    <div class="form-group" style=" margin-top: 3px">
                        <label for="exampleInputEmail2">是否活动：</label>
                        <select id="dlg_is_activity" class="form-control" style="width: 203px;" onchange="">
                            <option value="" <?php if ($activity == "-1") echo "selected=\"selected\""; ?>></option>
                            <option value="1" <?php if ($activity == "1") echo "selected=\"selected\""; ?>>是</option>
                            <option value="0" <?php if ($activity == "0") echo "selected=\"selected\""; ?>>否</option>
                        </select>
                        <label for="exampleInputEmail2">是否托管：</label>
                        <select id="dlg_is_trusteeship" class="form-control" style="width: 203px;" onchange="">
                            <option value="" <?php if ($trusteeship == "-1") echo "selected=\"selected\""; ?>></option>
                            <option value="1" <?php if ($trusteeship == "1") echo "selected=\"selected\""; ?>>是</option>
                            <option value="0" <?php if ($trusteeship == "0") echo "selected=\"selected\""; ?>>否</option>
                        </select>
                    </div>
                    <br>
                    <div class="form-group" style=" margin-top: 5px">
                        <label for="exampleInputEmail2">价格区间：</label>
                        <input type="number" min="0" class="form-control" style="width: 203px;" id="dlg_jg_form" placeholder=""
                               value="<?= $jg_form ?>">

                        <label for="exampleInputPassword2">To</label>
                        <input type="number" min="0" class="form-control" style="width: 203px;" id="dlg_jg_to"
                               placeholder=""
                               value="<?= $jg_to ?>">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="filter(2)"><i class="fa fa-search"></i> 搜索
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>
<!-- Modal GoodsHtml-->
<div class="modal fade" id="myModalHtmlDetail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">网页详情</h4>
            </div>
            <div id="htmldetail" class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭</button>
            </div>
        </div>
    </div>
</div>
<!-- Modal GoodsDetail-->
<div class="modal fade" id="myModalGoodsDetail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">商品详情</h4>
            </div>
            <div id="goodsdetail" class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭</button>
            </div>
        </div>
    </div>
</div>
